package com.xncoding.netty.server.http;

import com.xncoding.netty.coder.http.HttpMsgRequestDecoder;
import com.xncoding.netty.coder.http.HttpMsgResponseEncoder;
import com.xncoding.netty.handler.http.JsonHttpServerHandler;
import com.xncoding.netty.server.NettyServiceTemplate;
import io.netty.channel.ChannelHandler;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpRequestDecoder;
import io.netty.handler.codec.http.HttpResponseEncoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
public class JsonHttpServer extends NettyServiceTemplate {
	int port = 8888;
	String name = "Json Server";
	private String charset = "UTF-8";
	private int timeout = 60;
	
	public JsonHttpServer() {
	}
	public JsonHttpServer(int port) {
		this.port = port;
	}

	@Override
	protected ChannelHandler[] createHandlers() {
		return new ChannelHandler[] { 
				new HttpResponseEncoder(), 
				new HttpRequestDecoder(),
				new HttpObjectAggregator(1048576), //请求合并，如果没有，同一个http请求我们可能需要处理两次
				new HttpMsgResponseEncoder(charset, timeout), //数据解析器
				new HttpMsgRequestDecoder(charset),
//				new StringDecoder(), //String解码与编码器,将byte类型解析
//				new StringEncoder(),//String编码，序列化
				new JsonHttpServerHandler() };
	}

	@Override
	public int getPort() {
		return port;
	}

	@Override
	public String getName() {
		return name;
	}
	public void setPort(int port) {
		this.port = port;
	}
	public void setName(String name) {
		this.name = name;
	}
	

}

